﻿namespace Jxym.DBUtility.DEncrypt
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class RSACryption
    {
        public bool GetHash(FileStream objFile, ref string strHashData)
        {
            byte[] HashData = HashAlgorithm.Create("MD5").ComputeHash(objFile);
            objFile.Close();
            strHashData = Convert.ToBase64String(HashData);
            return true;
        }

        public bool GetHash(FileStream objFile, ref byte[] HashData)
        {
            HashData = HashAlgorithm.Create("MD5").ComputeHash(objFile);
            objFile.Close();
            return true;
        }

        public bool GetHash(string m_strSource, ref string strHashData)
        {
            HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
            byte[] Buffer = Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
            byte[] HashData = MD5.ComputeHash(Buffer);
            strHashData = Convert.ToBase64String(HashData);
            return true;
        }

        public bool GetHash(string m_strSource, ref byte[] HashData)
        {
            HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
            byte[] Buffer = Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
            HashData = MD5.ComputeHash(Buffer);
            return true;
        }

        public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPrivateKey);
            byte[] DypherTextBArray = rsa.Decrypt(DecryptString, false);
            return new UnicodeEncoding().GetString(DypherTextBArray);
        }

        public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPrivateKey);
            byte[] PlainTextBArray = Convert.FromBase64String(m_strDecryptString);
            byte[] DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
            return new UnicodeEncoding().GetString(DypherTextBArray);
        }

        public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPublicKey);
            byte[] PlainTextBArray = new UnicodeEncoding().GetBytes(m_strEncryptString);
            return Convert.ToBase64String(rsa.Encrypt(PlainTextBArray, false));
        }

        public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPublicKey);
            return Convert.ToBase64String(rsa.Encrypt(EncryptString, false));
        }

        public void RSAKey(out string xmlKeys, out string xmlPublicKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            xmlKeys = rsa.ToXmlString(true);
            xmlPublicKey = rsa.ToXmlString(false);
        }

        public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
        {
            byte[] HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPublic);
            RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
            RSADeformatter.SetHashAlgorithm("MD5");
            byte[] DeformatterData = Convert.FromBase64String(p_strDeformatterData);
            return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
        }

        public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
        {
            byte[] HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPublic);
            RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
            RSADeformatter.SetHashAlgorithm("MD5");
            return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
        }

        public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPublic);
            RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
            RSADeformatter.SetHashAlgorithm("MD5");
            byte[] DeformatterData = Convert.FromBase64String(p_strDeformatterData);
            return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
        }

        public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPublic);
            RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
            RSADeformatter.SetHashAlgorithm("MD5");
            return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
        }

        public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPrivate);
            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
            RSAFormatter.SetHashAlgorithm("MD5");
            byte[] EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
            m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
            return true;
        }

        public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
        {
            byte[] HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPrivate);
            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
            RSAFormatter.SetHashAlgorithm("MD5");
            EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
            return true;
        }

        public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
        {
            byte[] HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPrivate);
            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
            RSAFormatter.SetHashAlgorithm("MD5");
            byte[] EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
            m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
            return true;
        }

        public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(p_strKeyPrivate);
            RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
            RSAFormatter.SetHashAlgorithm("MD5");
            EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
            return true;
        }
    }
}

